﻿#include <QFile>
#include <QTcpSocket>
#include <QDebug>
#include "CInterface_global.h"


Record ServiceDoCommand(const char* json) {
    qDebug() << "ServiceDoCommand send" << json;

    Record ret = Literal::Null;
    QByteArray byteArray(json);

    int slen = byteArray.length();
    *(int*)szBuffer = slen;
    memcpy(szBuffer + 4, byteArray.data(), byteArray.length());
    szBuffer[4 + byteArray.length()] = 0;

    const char* ip = QString(CsGetVCallConfig("可视化配置"))!=""?CsGetVCallConfig("serviceIP"):CsGetConfig("评价服务器IP");
    int port = QString(CsGetVCallConfig("可视化配置"))!=""?QString(CsGetVCallConfig("servicePort")).toInt():QString(CsGetConfig("评价服务器Port")).toInt();
    QTcpSocket socket;
    socket.connectToHost(ip, port);
    if(!socket.waitForConnected(500)) goto RetPoint;

    //写出命令
    if(!socket.write(szBuffer, slen + 4)) {
        goto RetPoint;
    }
    if(!socket.waitForBytesWritten(1000)) goto RetPoint;

    //读返回长度
    if(!socket.waitForReadyRead(5000)) goto RetPoint;
    if(!socket.read((char*)&slen, 4)) {
        goto RetPoint;
    }

    //读返回json
    if(!socket.read(szBuffer, slen)) {
        goto RetPoint;
    }

    szBuffer[slen] = 0;
    //返回命令运行结果UTF8
    ret.FromString(szBuffer, slen);

RetPoint:
    qDebug() << "ServiceDoCommand return Size=" << ret.Size() << "Json=" << QString::fromUtf8(ret.ToString());
    return ret;
}

static Record clientConfig;
static Record serverConfig;
const char* CsGetConfig(const char* key) {
    if(key == NULL || key[0] == 0) return "";

    if(clientConfig.Size() == 0)
    {
        QFile file("config.txt");
        if(file.open(QIODevice::ReadOnly)) {
            QByteArray data = file.readAll();
            file.close();
            int sindex = data.indexOf('{');
            if(sindex > 0) {
                data = data.mid(sindex);
            } else if(sindex < 0) {
                data = "{}";
            }
            qDebug() << data;
            clientConfig.FromString(data, data.length());
            qDebug() << clientConfig.ToString();
        } else {
            qDebug() << QString("打开配置文件config.txt失败");
            exit(100);
        }
    }
    if(clientConfig.Exists(key)) {
        return clientConfig[key];
    } else {
        qDebug() << key << clientConfig.ToString();
    }

    if(serverConfig.Size() == 0) {
        Record cmd;
        cmd["cmd"] = "GetPJClientConfig";
        serverConfig = ServiceDoCommand(cmd.ToString());
    }

    if(!serverConfig.Exists("暂停弹出提示")) {
        serverConfig["暂停弹出提示"] = "";
    }

    if(!serverConfig.Exists("禁用弹出提示框")) {
        serverConfig["禁用弹出提示框"] = "0";
    }

    if(serverConfig.Exists(key)) {
        return serverConfig[key];
    } else {
        qDebug() << key << serverConfig.ToString();
    }

    return "";
}
static Record VCallClientConfig;
const char* CsGetVCallConfig(const char* key) {
    if(key == NULL || key[0] == 0) return "";

    if(VCallClientConfig.Size() == 0)
    {
        QFile file("VCall.txt");
        if(file.open(QIODevice::ReadOnly)) {
            QByteArray data = file.readAll();
            file.close();
            int sindex = data.indexOf('{');
            if(sindex > 0) {
                data = data.mid(sindex);
            } else if(sindex < 0) {
                data = "{}";
            }
            VCallClientConfig.FromString(data, data.length());
            qDebug() << "-------配置文件VCall.txt的内容："<<VCallClientConfig.ToString();
        } else {
            qDebug() << QString("打开配置文件VCall.txt失败");
            exit(100);
        }
    }
    if(VCallClientConfig.Exists(key)) {
        return VCallClientConfig[key];
    } else {
        qDebug()<<"------VCall.txt不存在配置项：" << key;
    }

    return "";
}
